package interfacegen.java;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import sdmakerz_v1.coresyntax.SDMakerV1Utils;
import sdmakerz_v1.coresyntax.generator.interfaces.IContext;
import sdmakerz_v1.coresyntax.generator.interfaces.IInterfaceData;
import sdmakerz_v1.coresyntax.generator.interfaces.IMethodData;
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerFolderLevelGen;

@SuppressWarnings("all")
public class JavaInterfaceGen implements ISDMakerFolderLevelGen {
  public void doGenerate(final IContext context, final IFileSystemAccess fsa) {
    List<IInterfaceData> _interfacesDatas = context.getInterfacesDatas();
    for (final IInterfaceData interfaceData : _interfacesDatas) {
      String _qualifiedName = interfaceData.getQualifiedName();
      String _replaceAll = _qualifiedName.replaceAll("\\.", "/");
      String _plus = ("java-interface-generator-gen/" + _replaceAll);
      String _plus_1 = (_plus + ".java");
      CharSequence _compileInterface = this.compileInterface(interfaceData);
      fsa.generateFile(_plus_1, _compileInterface);
    }
  }
  
  public CharSequence compileInterface(final IInterfaceData interfaceData) {
    String qualifiedName = interfaceData.getQualifiedName();
    List<String> clazzs = null;
    int i = 0;
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package ");
    String _computePackageName = SDMakerV1Utils.computePackageName(qualifiedName);
    _builder.append(_computePackageName, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    {
      List<String> _computeNeededImports = this.computeNeededImports(interfaceData);
      for(final String importQualifiedName : _computeNeededImports) {
        _builder.append("import ");
        _builder.append(importQualifiedName, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    _builder.append("public interface ");
    String _computeSimpleName = SDMakerV1Utils.computeSimpleName(qualifiedName);
    _builder.append(_computeSimpleName, "");
    _builder.append("{");
    _builder.newLineIfNotEmpty();
    CharSequence charSequenceResult = _builder;
    List<IMethodData> _allMethodData = interfaceData.getAllMethodData();
    for (final IMethodData methodData : _allMethodData) {
      {
        List<String> _paramsRealType = methodData.getParamsRealType();
        clazzs = _paramsRealType;
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("public Object ");
        String _methodName = methodData.getMethodName();
        _builder_1.append(_methodName, "");
        _builder_1.append("(");
        String _plus = (charSequenceResult + _builder_1.toString());
        charSequenceResult = _plus;
        i = 0;
        int _size = clazzs.size();
        int _minus = (_size - 1);
        boolean _lessThan = (i < _minus);
        boolean _while = _lessThan;
        while (_while) {
          {
            StringConcatenation _builder_2 = new StringConcatenation();
            String _get = clazzs.get(i);
            String _computeSimpleName_1 = SDMakerV1Utils.computeSimpleName(_get);
            _builder_2.append(_computeSimpleName_1, "");
            _builder_2.append(" arg");
            _builder_2.append(i, "");
            _builder_2.append(", ");
            String _plus_1 = (charSequenceResult + _builder_2.toString());
            charSequenceResult = _plus_1;
            int _plus_2 = (i + 1);
            i = _plus_2;
          }
          int _size_1 = clazzs.size();
          int _minus_1 = (_size_1 - 1);
          boolean _lessThan_1 = (i < _minus_1);
          _while = _lessThan_1;
        }
        int _size_1 = clazzs.size();
        boolean _greaterThan = (_size_1 > 0);
        if (_greaterThan) {
          StringConcatenation _builder_2 = new StringConcatenation();
          String _get = clazzs.get(i);
          String _computeSimpleName_1 = SDMakerV1Utils.computeSimpleName(_get);
          _builder_2.append(_computeSimpleName_1, "");
          _builder_2.append(" arg");
          _builder_2.append(i, "");
          String _plus_1 = (charSequenceResult + _builder_2.toString());
          charSequenceResult = _plus_1;
        }
        StringConcatenation _builder_3 = new StringConcatenation();
        _builder_3.append(");");
        _builder_3.newLine();
        String _plus_2 = (charSequenceResult + _builder_3.toString());
        charSequenceResult = _plus_2;
      }
    }
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("}");
    String _plus = (charSequenceResult + _builder_1.toString());
    charSequenceResult = _plus;
    return charSequenceResult;
  }
  
  public List<String> computeNeededImports(final IInterfaceData interfaceData) {
    ArrayList<String> _arrayList = new ArrayList<String>();
    List<String> computedImports = _arrayList;
    List<IMethodData> _allMethodData = interfaceData.getAllMethodData();
    for (final IMethodData methodData : _allMethodData) {
      List<String> _paramsRealType = methodData.getParamsRealType();
      for (final String clazz : _paramsRealType) {
        boolean _and = false;
        boolean _and_1 = false;
        boolean _and_2 = false;
        boolean _and_3 = false;
        boolean _and_4 = false;
        boolean _and_5 = false;
        boolean _contains = SDMakerV1Utils.contains(computedImports, clazz);
        boolean _equals = (_contains == false);
        if (!_equals) {
          _and_5 = false;
        } else {
          String _qualifiedName = interfaceData.getQualifiedName();
          int _compareTo = clazz.compareTo(_qualifiedName);
          boolean _notEquals = (_compareTo != 0);
          _and_5 = (_equals && _notEquals);
        }
        if (!_and_5) {
          _and_4 = false;
        } else {
          String _name = Boolean.class.getName();
          int _compareTo_1 = clazz.compareTo(_name);
          boolean _notEquals_1 = (_compareTo_1 != 0);
          _and_4 = (_and_5 && _notEquals_1);
        }
        if (!_and_4) {
          _and_3 = false;
        } else {
          String _name_1 = Integer.class.getName();
          int _compareTo_2 = clazz.compareTo(_name_1);
          boolean _notEquals_2 = (_compareTo_2 != 0);
          _and_3 = (_and_4 && _notEquals_2);
        }
        if (!_and_3) {
          _and_2 = false;
        } else {
          String _name_2 = Double.class.getName();
          int _compareTo_3 = clazz.compareTo(_name_2);
          boolean _notEquals_3 = (_compareTo_3 != 0);
          _and_2 = (_and_3 && _notEquals_3);
        }
        if (!_and_2) {
          _and_1 = false;
        } else {
          String _name_3 = Character.class.getName();
          int _compareTo_4 = clazz.compareTo(_name_3);
          boolean _notEquals_4 = (_compareTo_4 != 0);
          _and_1 = (_and_2 && _notEquals_4);
        }
        if (!_and_1) {
          _and = false;
        } else {
          String _name_4 = String.class.getName();
          int _compareTo_5 = clazz.compareTo(_name_4);
          boolean _notEquals_5 = (_compareTo_5 != 0);
          _and = (_and_1 && _notEquals_5);
        }
        if (_and) {
          computedImports.add(clazz);
        }
      }
    }
    return computedImports;
  }
}
